
export default {
  data() {
    return {
      $_sidebarElm: null
    }
  },
  mounted() {
    this.__resizeHandler = throttle(async () => {
      if (this.olMap) {
        this.$refs.map.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)

    this.$_sidebarElm = document.getElementsByClassName('sidebar-container')[0]
    this.$_sidebarElm && this.$_sidebarElm.addEventListener('transitionend', this.$_sidebarResizeHandler)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.__resizeHandler)

    this.$_sidebarElm && this.$_sidebarElm.removeEventListener('transitionend', this.$_sidebarResizeHandler)
  },
  methods: {
    // use $_ for mixins properties
    // https://vuejs.org/v2/style-guide/index.html#Private-property-names-essential
    $_sidebarResizeHandler(e) {
      if (e.propertyName === 'width') {
        this.__resizeHandler()
      }
    }
  }
}
export function throttle(func, wait, mustRun) {
  let timeout = null,
    startTime = new Date()

  return function () {
    var context = this,
      args = arguments,
      curTime = new Date()

    clearTimeout(timeout)
    // 如果达到了规定的触发时间间隔，触发 handler
    if (mustRun > 0 && curTime - startTime >= mustRun) {
      func.apply(context, args)
      startTime = curTime
      if (!timeout) context = args = null
      // 没达到触发间隔，重新设定定时器
    } else {
      timeout = setTimeout(() => {
        if (!timeout) context = args = null
        return func.apply(context, args)
      }, wait)
    }
  }
}